package com.apps.apicontroller;

import com.alipay.api.internal.util.AlipaySignature;
import com.apps.common.model.po.ApiResult;
import com.apps.config.AliPayConfig;
import com.apps.config.WebSiteConfig;
import com.apps.config.WxPayConfig;
import com.apps.helper.UserConextHelper;
import com.apps.module.pay.model.vo.KupaPayDataVo;
import com.apps.order.enums.OrderStatus;
import com.apps.order.enums.PayType;
import com.apps.order.model.po.KupaVipGoodsOrder;
import com.apps.order.service.IKupaOrder;
import com.apps.order.service.IKupaVipGoodsOrderService;
import com.apps.sexctption.DataException;
import com.apps.sexctption.NullException;
import com.apps.thirdparty.pay.alipay.model.po.KupaAlipayCallback;
import com.apps.thirdparty.pay.alipay.service.IKupaAlipayService;
import com.apps.thirdparty.pay.alipay.utils.IKupaAlipayUtils;
import com.apps.thirdparty.pay.wxpay.model.po.KupaWxpayCallback;
import com.apps.thirdparty.pay.wxpay.model.po.KupaWxpayReturn;
import com.apps.thirdparty.pay.wxpay.service.IKupaWxPayService;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.*;

@Slf4j
@Api(description = "支付回调接口")
@RestController
@RequestMapping("/api/pay")
public class PayCallbackApiController
{


    @Autowired
    private IKupaAlipayService alipayService;

    @Autowired
    private IKupaWxPayService wxPayService;

    @Autowired
    private IKupaVipGoodsOrderService vipGoodsOrderService;

    @Autowired
    List<IKupaOrder> kupaOrders;




    @ApiOperation(value = "微信网站支付回调")
    @ResponseBody
    @PostMapping("/WechatNativePay")
    String WechatNativePay(HttpServletRequest request)
    {
        log.info("微信支付回调");
        KupaWxpayCallback wxpayCallback=wxPayService.HandleCallBack(request);
        if(null == wxpayCallback)
            throw new NullException("微信回调失败");
        for(IKupaOrder order:kupaOrders)
            order.PaySuccess(wxpayCallback.getTransaction_id(),wxpayCallback.getOut_trade_no(),wxpayCallback.getTotal_fee(), PayType.WECHAT);
        return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }


    @ApiOperation(value = "支付宝网站支付回调")
    @ResponseBody
    @PostMapping("/AlipayNativePay")
    String AlipayNativePay(HttpServletRequest request,
                           HttpServletResponse response)
    {
        log.info("支付宝支付回调");
        KupaAlipayCallback alipayCallback=alipayService.HandleCallBack(request);
        if(null == alipayCallback) {
            throw new DataException("支付宝回调签名错误");
        }
        for(IKupaOrder order:kupaOrders)
            order.PaySuccess(alipayCallback.getTrade_no(),alipayCallback.getOut_trade_no(),alipayCallback.getTotal_amount(), PayType.ALIPAY);
        return "success";
    }

}
